package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.tool.PlatformTools;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TZhgdDeviceToolsInfoMapper;
import com.ruoyi.system.domain.TZhgdDeviceToolsInfo;
import com.ruoyi.system.service.ITZhgdDeviceToolsInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工器具台账Service业务层处理
 * 
 * @author wuyafeng
 * @date 2025-01-16
 */

@Service
public class TZhgdDeviceToolsInfoServiceImpl implements ITZhgdDeviceToolsInfoService 
{
    private static final Logger log = LoggerFactory.getLogger(TZhgdDeviceToolsInfoServiceImpl.class);
    @Autowired
    private TZhgdDeviceToolsInfoMapper tZhgdDeviceToolsInfoMapper;

    /**
     * 查询工器具台账
     * 
     * @param id 工器具台账主键
     * @return 工器具台账
     */
    @Override
    public TZhgdDeviceToolsInfo selectTZhgdDeviceToolsInfoById(String id)
    {
        return tZhgdDeviceToolsInfoMapper.selectTZhgdDeviceToolsInfoById(id);
    }

    /**
     * 查询工器具台账列表
     * 
     * @param tZhgdDeviceToolsInfo 工器具台账
     * @return 工器具台账
     */
    @Override
    public List<TZhgdDeviceToolsInfo> selectTZhgdDeviceToolsInfoList(TZhgdDeviceToolsInfo tZhgdDeviceToolsInfo)
    {
        return tZhgdDeviceToolsInfoMapper.selectTZhgdDeviceToolsInfoList(tZhgdDeviceToolsInfo);
    }

    /**
     * 新增工器具台账
     * 
     * @param tZhgdDeviceToolsInfo 工器具台账
     * @return 结果
     */
    @Override
    public int insertTZhgdDeviceToolsInfo(TZhgdDeviceToolsInfo tZhgdDeviceToolsInfo)
    {
        tZhgdDeviceToolsInfo.setId(PlatformTools.get20UUID());
        tZhgdDeviceToolsInfo.setCreateUser("杨旭");
        tZhgdDeviceToolsInfo.setCreateTime(DateUtils.getNowDate());
        return tZhgdDeviceToolsInfoMapper.insertTZhgdDeviceToolsInfo(tZhgdDeviceToolsInfo);
    }

    /**
     * 修改工器具台账
     * 
     * @param tZhgdDeviceToolsInfo 工器具台账
     * @return 结果
     */
    @Override
    public int updateTZhgdDeviceToolsInfo(TZhgdDeviceToolsInfo tZhgdDeviceToolsInfo)
    {
        tZhgdDeviceToolsInfo.setUpdateTime(DateUtils.getNowDate());
        return tZhgdDeviceToolsInfoMapper.updateTZhgdDeviceToolsInfo(tZhgdDeviceToolsInfo);
    }

    /**
     * 批量删除工器具台账
     * 
     * @param ids 需要删除的工器具台账主键
     * @return 结果
     */
    @Override
    public int deleteTZhgdDeviceToolsInfoByIds(String ids)
    {
        return tZhgdDeviceToolsInfoMapper.deleteTZhgdDeviceToolsInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工器具台账信息
     * 
     * @param id 工器具台账主键
     * @return 结果
     */
    @Override
    public int deleteTZhgdDeviceToolsInfoById(String id)
    {
        return tZhgdDeviceToolsInfoMapper.deleteTZhgdDeviceToolsInfoById(id);
    }
    /**
     * 获取未同步数据
     *
     * @return 结果
     */
    @Override
    public List<TZhgdDeviceToolsInfo> uploadToolsInfoById()
    {
        return tZhgdDeviceToolsInfoMapper.selectToolsInfoNoSync();
    }
    @Async
    @Override
    public String uploadToolsInfo(String url,String projectId){
        List<TZhgdDeviceToolsInfo> toolsInfos = uploadToolsInfoById();
        if(toolsInfos==null||toolsInfos.size()==0){
            log.info("toolsInfo no data");
            return "toolsInfo no data";
        }
        toolsInfos.forEach(x -> {
            x.setProjectId(projectId);
        });
        JSONObject obj = new JSONObject();
         obj.put("toolsInfo",toolsInfos);
        try {
            String result = HttpUtils.postCall(url, obj.toString());
            JSONObject jsonData = JSONObject.parseObject(result);
            if("success".equals(jsonData.get("state"))){
                toolsInfos.forEach( toolsInfo->{
                    toolsInfo.setSyncFlag(1);
                    tZhgdDeviceToolsInfoMapper.updateTZhgdDeviceToolsInfo(toolsInfo);
                });
            }
            log.info("请求结果"+result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "工器具类型同步任务执行";
    }

}
